package me.lc.superDown.core;

import java.util.Timer;
import java.util.TimerTask;

import com.wiyun.engine.nodes.Director;

import me.lc.superDown.layer.GameLayer;
import me.lc.superDown.scene.GameScene;
import me.lc.superDown.util.AudioHelper;
import android.app.Activity;
import android.content.SharedPreferences.Editor;

public class GameManager {
	public static GameManager instance = new GameManager();

	private String audioConfigKey = "audioKey";
	private String currentHighKey = "currentHigh";
	public boolean isAudioOn = true;

	private GameManager() {
		initSettings();
	}

	private void initSettings() {
		isAudioOn = GameSystem.SharedSettings.getBoolean(audioConfigKey, true);
		currentHigh = GameSystem.SharedSettings.getInt(currentHighKey, 0);
	}

	public void saveAudioConfig() {
		Editor editor = GameSystem.SharedSettings.edit();
		editor.putBoolean(audioConfigKey, isAudioOn);
		editor.commit();
	}

	/**
	 * 保存当前最高分
	 */
	public void saveCurrentHigh() {
		Editor editor = GameSystem.SharedSettings.edit();
		editor.putInt(currentHighKey, currentHigh);
		editor.commit();
	}

	GameLayer gameLayer;

	public void startMainMenu(GameLayer gameLayer) {
		if (timeCountTimer != null) {
			timeCountTimer.cancel();
			timeCountTimer = null;
		}
		GameScene.make().mainLayer.setVisible(true);
		GameScene.make().finishLayer.setVisible(false);
		GameScene.make().pauseLayer.setVisible(false);
		isStarted = false;
		isPaused = false;
		isFinished = false;
		this.gameLayer = gameLayer;
		GameMap.instance.initMainMenu(gameLayer);
		if (GameManager.instance.isAudioOn) {
			AudioHelper.playMusicInGame();
		}
	}

	public void startNewGame(GameLayer gameLayer) {

		if (timeCountTimer != null) {
			timeCountTimer.cancel();
			timeCountTimer = null;
		}

		GameScene.make().mainLayer.setVisible(false);
		GameScene.make().finishLayer.setVisible(false);
		GameScene.make().pauseLayer.setVisible(false);
		isStarted = true;
		isPaused = false;
		isFinished = false;
		this.gameLayer = gameLayer;
		gameLayer.clockBar.setSecond(0);
		gameLayer.coinBar.setCoinCount(0);
		speedBonus = 1;
		currentSeconds = 0;
		gameTimerScore = 0;

		coinCount = 0;

		isSpeedDown = false;
		isSpeedUp = false;
		speedChangeSeconds = 0;
		toolSpeedBonus = 1;

		GameMap.instance.initNewGameMap(gameLayer);

		timeCountTimer = new Timer();
		timeCountTimer.schedule(new SecondsCountTask(), 1000, 1000);
		isStarted = true;

		if (GameManager.instance.isAudioOn) {
			AudioHelper.playMusicInGame();
		}
	}

	public float speedBonus = 1;

	Timer timeCountTimer;
	int currentSeconds = 0;

	public boolean isStarted = false;
	public boolean isPaused = false;
	public boolean isFinished = false;
	// 游戏得分相关属性
	public int coinCount = 0;
	public int gameTimerScore = 0;
	public int currentHigh = 0;

	// 道具相关属性
	public boolean isSpeedDown;
	public boolean isSpeedUp;
	private int speedChangeSeconds;
	public float toolSpeedBonus = 1;

	private Object speedChangeSyncObj = new Object();

	/**
	 * 结束游戏
	 */
	public void finishGame() {
		// 显示游戏结束画面
		AudioHelper.stopMusic();
		isStarted = false;
		isFinished = true;
		GameScene.make().finishLayer.setVisible(true);
		GameScene.make().finishLayer.setScore(currentSeconds, coinCount);
		AudioHelper.finish();
	}

	class SecondsCountTask extends TimerTask {

		@Override
		public synchronized void run() {
			if (isStarted && !isPaused) {
				currentSeconds++;
				gameLayer.clockBar.setSecond(currentSeconds);
				if (currentSeconds == 16) {
					GameMap.instance.resetPlatformMoveInterval(20);
				} else if (currentSeconds == 31) {
					GameMap.instance.resetPlatformMoveInterval(16);
				} else if (currentSeconds == 51) {
					GameMap.instance.resetPlatformMoveInterval(14);
				} else if (currentSeconds == 76) {
					GameMap.instance.resetPlatformMoveInterval(11);
				} else if (currentSeconds == 91) {
					GameMap.instance.resetPlatformMoveInterval(9);
				} else if (currentSeconds == 105) {
					GameMap.instance.resetPlatformMoveInterval(7);
				}
				synchronized (speedChangeSyncObj) {
					if (speedChangeSeconds > 0) {
						speedChangeSeconds--;
						if (speedChangeSeconds == 2) {
							if (isSpeedDown) {
								Director.getInstance().runOnGLThread(
										new Runnable() {

											@Override
											public void run() {
												GameMap.instance.runGuy
														.setSpeedDownBlink();
											}
										});

							}
							if (isSpeedUp) {
								Director.getInstance().runOnGLThread(
										new Runnable() {

											@Override
											public void run() {
												GameMap.instance.runGuy
														.setSpeedUpBlink();
											}
										});

							}
						}
						if (speedChangeSeconds <= 0) {
							isSpeedDown = false;
							isSpeedUp = false;
							toolSpeedBonus = 1.0f;
							Director.getInstance().runOnGLThread(
									new Runnable() {

										@Override
										public void run() {
											GameMap.instance.runGuy
													.closeSpeedTool();
										}
									});

						}
					}
				}

			}
		}
	}

	public void hitSpeedUpTool() {
		isSpeedDown = false;
		isSpeedUp = true;
		toolSpeedBonus = 1.5f;
		synchronized (speedChangeSyncObj) {
			speedChangeSeconds = 10;
		}
		GameMap.instance.runGuy.setSpeedUpTool();

	}

	public void hitSpeedDownTool() {
		isSpeedDown = true;
		isSpeedUp = false;
		toolSpeedBonus = 0.5f;
		synchronized (speedChangeSyncObj) {
			speedChangeSeconds = 10;
		}
		GameMap.instance.runGuy.setSpeedDownTool();
	}

	public void addCoin() {
		coinCount++;
		gameLayer.coinBar.setCoinCount(coinCount);
	}
}
